/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.oracleandsxnd.erp.seting.web;

import com.oracleandsxnd.erp.common.persistence.Page;
import com.oracleandsxnd.erp.common.persistence.TableSplitResult;
import com.oracleandsxnd.erp.common.utils.StringUtils;
import com.oracleandsxnd.erp.common.web.AjaxResult;
import com.oracleandsxnd.erp.common.web.BaseController;
import com.oracleandsxnd.erp.seting.entity.Area;
import com.oracleandsxnd.erp.seting.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 地区设置Controller
 * @author 地区设置
 * @version 2019-12-17
 */
@Controller
@RequestMapping(value = "seting_area")
public class AreaController extends BaseController {

	@Autowired
	private AreaService areaService;

	@RequestMapping(value = {""})
	public String list(Model model) {
		List<Area> areas=areaService.findList(new Area());
		model.addAttribute("areas",areas);
		return "erp/seting/areaList";
	}
	@RequestMapping(value = {"listData"})
	@ResponseBody
	public Object listData(Area area , HttpServletRequest request, HttpServletResponse response){
		Page<Area> page = areaService.findPage(new Page<Area>(request, response), area);
		TableSplitResult<Area> result=new TableSplitResult<Area>(page.getPageNo(), page.getCount(), page.getList());
		return  result;
	}


	@RequestMapping(value = "form")
	public String form(String id, Model model) {
		Area area=null;
		if (StringUtils.isNotBlank(id)){
			area = areaService.get(id);
		}
		if (area == null){
			area = new Area();
		}
		model.addAttribute("area",area);
		return "erp/seting/areaForm";
	}


	@RequestMapping(value = "save")
	@ResponseBody
	public AjaxResult save(Area area) {

		areaService.save(area);
		return success();

	}

	@RequestMapping(value = "delete")
	@ResponseBody
	public AjaxResult delete(Area area, RedirectAttributes redirectAttributes) {
		areaService.delete(area);
		return success();
	}
	
}